<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item>
        <el-button @click="showFrom">添加角色</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="ptType" label="名称" width="180" />
      <el-table-column prop="meno" label="描述" width="180" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="status(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="showMenus(scope.row)"
            >分配权限</el-button
          >
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size="limit"
      :current-page="page"
      :page-sizes="[2, 10, 20, 30]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-position="right">
        <el-form :model="form">
          <el-form-item label="名称" label-width="100px">
            <el-input v-model="form.ptType" autocomplete="off" />
          </el-form-item>
          <el-form-item label="描述" label-width="100px">
            <el-input v-model="form.meno" autocomplete="off" />
          </el-form-item>

          <el-form-item label="状态" label-width="100px">
            <el-switch
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addORupdate">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="分配权限" :visible.sync="dialog">
      <el-input
        placeholder="输入关键字进行搜索   使用拖拽功能编辑父子菜单"
        v-model="filterText"
      >
      </el-input>
      <br />
      <el-tree
        :data="menus"
        :props="defaultProps"
        :filter-node-method="filterNode"
        ref="tree"
        show-checkbox
        node-key="mid"
        :default-checked-keys="defaultChecked"
      >
      </el-tree>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="setMenus">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      dialogFormVisible: false,
      title: "添加分店",
      form: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      value1: "",
      value2: "",
      value3: "",
      dialog: false,
      menus: [],
      defaultProps: {
        label: "mname",
        children: "menuList",
      },
      filterText: "",
      defaultChecked: [],
      rid: null,
    };
  },
  created() {
    this.getPage();
    this.getMenus();
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  computed: {},
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.mname.indexOf(value) !== -1;
    },
    showMenus(row) {
      this.dialog = true;
      this.rid = row.rid;
      this.$http
        .get(`http://127.0.0.1/menu/getByRoleId/${row.rid}`)
        .then(({ data }) => {
          this.$refs.tree.setCheckedKeys(
            data.menuList.map((menu) => {
              return menu.mid;
            })
          );
        });
    },
    getMenus() {
      this.$http.get(`http://127.0.0.1/menu/tree`).then((res) => {
        this.menus = res.data.menuTree;
      });
    },
    setMenus() {
      if (this.rid == null) {
        return;
      }
      this.$http
        .post(`http://127.0.0.1/menu/setByRoleId/${this.rid}`, {
          menuIds: this.$refs.tree.getCheckedKeys(),
        })
        .then(({ data }) => {
              this.$message({
                type: 'success',
                message: '保存成功',
              });
        });
    },
    status(row) {
      this.$http
        .post(`http://127.0.0.1/position/status/${row.rid}`)
        .then(({ data }) => {
          this.getPage();
        });
    },
    showFrom() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    getPage() {
      this.$http
        .post(`http://127.0.0.1/position/page/${this.page}/${this.limit}`)
        .then(({ data }) => {
          this.tableData = data.pageInfo.list;
          this.total = data.pageInfo.total;
        });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getPage();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getPage();
    },
    handleEdit(indedx, row) {
      this.$http
        .get(`http://127.0.0.1/position/getById/${row.rid}`)
        .then(({ data }) => {
          this.dialogFormVisible = true;
          this.form = data.menu;
        });
    },
    handleDelete(indedx, row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .delete(`http://127.0.0.1/position/delete/${row.rid}`)
            .then(({ data }) => {
              this.getPage();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addORupdate() {
      if (this.form.rid != null) {
        this.$http
          .post(`http://127.0.0.1/position/updateById`, this.form)
          .then(({ data }) => {
            this.getPage();
            this.dialogFormVisible = false;
          });
      } else {
        this.$http
          .put(`http://127.0.0.1/position/save`, this.form)
          .then(({ data }) => {
            this.getPage();
            this.dialogFormVisible = false;
          });
      }
    },
  },
};
</script>
<style  scoped>
</style>